package com.qm.console.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutBucketImageRequest;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.google.common.io.Files;

import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.annotation.PostConstruct;
import javax.imageio.ImageIO;

/**
 * //                       _oo0oo_
 * //                      o8888888o
 * //                      88" . "88
 * //                      (| -_- |)
 * //                      0\  =  /0
 * //                    ___/`---'\___
 * //                  .' \\|     |// '.
 * //                 / \\|||  :  |||// \
 * //                / _||||| -:- |||||- \
 * //               |   | \\\  -  /// |   |
 * //               | \_|  ''\---/''  |_/ |
 * //               \  .-\__  '-'  ___/-. /
 * //             ___'. .'  /--.--\  `. .'___
 * //          ."" '<  `.___\_<|>_/___.' >' "".
 * //         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 * //         \  \ `_.   \_ __\ /__ _/   .-` /  /
 * //     =====`-.____`.___ \_____/___.-`___.-'=====
 * //                       `=---='
 * //
 * //
 * //     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * //
 * //               佛祖保佑         永无BUG
 * <p>
 * com.qm.console.utils
 * Created on 2021/7/22.
 */
@Component
public class AliOssUtil {

   @Value("${aliyun.region}")
   private String region;
   @Value("${aliyun.accessKeyId}")
   private String accessKeyId;
   @Value("${aliyun.accessKeySecret}")
   private String accessKeySecret;
   @Value("${aliyun.bucket}")
   private String bucket;

   private static final String ENDPOINT_FORMAT = "http://%s.aliyuncs.com";
   private static final String IMAGE_URL_FORMAT = "http://%s.%s.aliyuncs.com/%s";

   private OSS client;

   @PostConstruct
   private void init() {
      String endpoint = String.format(ENDPOINT_FORMAT, region);
      client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
   }

   public String uploadImage(File imageFile) {
      String extension = Files.getFileExtension(imageFile.getPath());
      String key = new Md5Hash(imageFile.getAbsolutePath()) + "." + extension;
      return uploadImage(imageFile, key);
   }

   public String uploadImage(File imageFile, String key) {
      PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, key, imageFile);
      putObjectRequest.setLogEnabled(false);
      client.putObject(putObjectRequest);
      return key;
   }

   public Point imageSize(File imageFile) {
      try {
         BufferedImage image = ImageIO.read(imageFile);
         return new Point(image.getWidth(), image.getHeight());
      } catch (IOException e) {
      }
      return null;
   }

   public static void main(String[] args) {
      AliOssUtil util = new AliOssUtil();
      util.init();
      String result = util.uploadImage(new File("/Users/danliu/Desktop/HOT TOYS/PPS 钢铁侠MK-XLII/banner/(5).jpg"));
      System.out.println(result);
   }
}
